package com.sharkpro.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.cell.CellUtil;
import com.sharkpro.entity.ElecExceptionRecords;
import com.sharkpro.service.ElecExceptionRecordsService;
import com.sharkpro.service.UtilsService;
import com.sharkpro.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jianxin.wei
 * @description 小工具页面
 * @date 2024/12/30 13:16
 */
@RestController
@RequestMapping("utils")
public class UtilsController {

    @Resource
    UtilsService utilsService;


    @Resource
    ElecExceptionRecordsService elecExceptionRecordsService;
    /**
     * base64转成图片
     */
    @RequestMapping("base64ToImage")
    public void base64ToImage(@RequestBody JSONObject jsonObject, HttpServletResponse response) throws IOException {
        utilsService.base64ToImage(jsonObject,response);
    }

    /**
     * 格式化json,返回的时候获取data的内容即可
     */
    @RequestMapping("standardJson")
    public ResultUtil standardJson(@RequestBody JSONObject jsonObject) {
        String jsonStr = JSONUtil.toJsonStr(jsonObject);
        JSONObject object = JSONUtil.parseObj(jsonStr);
        return new ResultUtil().success(object);
    }

    /**
     * 导入出账异常数据
     */
    @RequestMapping("/importExceptions")
    public void importExceptions(@RequestBody MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<Sheet> sheets = reader.getSheets();
        Sheet sheet = sheets.get(0);
        List<ElecExceptionRecords> list = new ArrayList<>();
        List<List<Object>> read = reader.read();
        for (int i = 1; i < read.size(); i++) {
            ElecExceptionRecords entity = new ElecExceptionRecords();
//            entity.setPolicyId(getData(reader,1,i)==null?null:Long.valueOf(getData(reader,1,i)));
            entity.setPolicyName(getData(reader,2,i));
            entity.setConsNo(getData(reader,3,i));
            entity.setConsName(getData(reader,4,i));
            entity.setElecAddr(getData(reader,5,i));
            entity.setOrgNo(getData(reader,6,i));
            entity.setOrgName(getData(reader,7,i));
            String consType =getData(reader,8,i);
            entity.setConsType("高压".equals(consType)?"01":"低压非居民".equals(consType)?"02":
                    "低压居民".equals(consType)?"03":"04");
            entity.setBillDate(getData(reader,9,i));
            entity.setBillDate("12");
            entity.setVoltageLevel(getData(reader,10,i)==null?null:Long.valueOf(getData(reader,10,i)));
            entity.setPlanNo(getData(reader,11,i));
            entity.setCalculateNo(getData(reader,12,i));
            entity.setMeterNo(getData(reader,13,i));
            entity.setExceptionType(getData(reader,14,i)==null?null:Long.valueOf(getData(reader,14,i)));
            entity.setCreateTime( reader.readCellValue(15,i)!=null?(Date)reader.readCellValue(15,i):null);
            entity.setCreateDate( reader.readCellValue(15,i)!=null?(Date)reader.readCellValue(15,i):null);
            entity.setCalculateTime(getData(reader,16,i));
            entity.setHandleResult(getData(reader,17,i)==null?null:Long.valueOf(getData(reader,17,i)));
            entity.setHandleTime(reader.readCellValue(18,i)!=null?(Date)reader.readCellValue(18,i):null);
            list.add(entity);
        }
        elecExceptionRecordsService.saveBatch(list);
        System.out.println();
    }

    public static String getData(ExcelReader reader , int x,int y){
        return String.valueOf(reader.readCellValue(x,y));
    }

    /**
     * 输出Excel表格
     */
    @RequestMapping("writerExcel")
    public ResultUtil writerExcel() {
        String path = "C:\\Users\\xiechang\\Desktop\\1.xlsx";
        FileUtil.del(path);
        ExcelWriter writer = new ExcelWriter(path);
        writer.writeCellValue(0, 0, "到货批次号");
        writer.writeCellValue(1, 0, "条形码");
        writer.writeCellValue(2, 0, "资产编号");
        writer.writeCellValue(3, 0, "出厂编号");
        JSONArray jsonArray = JSONUtil.parseArray("[1,2,3,4]");
        int size = jsonArray.size();
        if (size > 1) {
            writer.merge(1, size, 0, 0, "arrBatch", false);
        } else {
//            writer.writeCellValue(0, 1, arrBatch, false);
            Cell cell = writer.getOrCreateCell(0, 1);
            CellUtil.setCellValue(cell,"arrBatch",writer.getStyleSet(),false);
        }
        for (int i = 1; i < size + 1; i++) {
            writer.writeCellValue(1, i, jsonArray.getJSONObject(i - 1).getStr("barCode"));
            writer.writeCellValue(2, i, jsonArray.getJSONObject(i - 1).getStr("assetNo"));
            writer.writeCellValue(3, i, jsonArray.getJSONObject(i - 1).getStr("mfcNo"));
        }
        writer.passRows(1);
        for (int i = 0; i < 4; i++) {
//            writer.autoSizeColumn(i, true, 3f);
            writer.getSheet().setColumnWidth(i,(int)((float) writer.getSheet().getColumnWidth(i) * 3f));
        }
        writer.close();
        return null;
    }

    @RequestMapping("invoke")
    public ResultUtil invoke(){
        String post = HttpUtil.post("http://localhost:6888/user/getImage", "");
        JSONObject array = JSONUtil.parseObj(post);
        return new ResultUtil().success(post);
    }
}
